package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.AccidentAudit;
import cn.wolfcode.crm.domain.Claim;
import cn.wolfcode.crm.domain.ClaimItem;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AccidentAuditMapper;
import cn.wolfcode.crm.mapper.ClaimItemMapper;
import cn.wolfcode.crm.mapper.ClaimMapper;
import cn.wolfcode.crm.query.ClaimQueryObject;
import cn.wolfcode.crm.service.IClaimService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClaimServiceImpl implements IClaimService {
    @Autowired
    private ClaimMapper claimMapper;

    @Autowired
    private ClaimItemMapper claimItemMapper;

    @Autowired
    private AccidentAuditMapper accidentAuditMapper;


    public void deleteByPrimaryKey(Long id) {
        claimMapper.deleteByPrimaryKey(id);
    }

    public void insert(Claim claim) {
        Employee currentUser = UserContext.getCurrentUser();
        claim.setSn(claim.getReporter() + new Date().toString() );
        claim.setInputTime(new Date());
        claim.setInputUser(currentUser);
        claim.setState(false);
        claimMapper.insert(claim);

        //保存明细
        ClaimItem item = claim.getItem();
        item.setClaim(claim);
        item.setState(false);
        claimItemMapper.insert(item);

        //保存赔损到到审核表
        boolean state = false;
        accidentAuditMapper.insertClaimId(claim.getId(),state);
    }

    public Claim selectByPrimaryKey(Long id) {
        return claimMapper.selectByPrimaryKey(id);
    }

    public List<Claim> selectAll() {
        return claimMapper.selectAll();
    }

    public void updateByPrimaryKey(Claim record) {
        AccidentAudit accidentAudit =  accidentAuditMapper.selectAaByClaimId(record.getId());
        if (accidentAudit.getState() == true){
            throw new LogicException("数据已经审核,无法更改");
        }
        claimMapper.updateByPrimaryKey(record);
        ClaimItem item = record.getItem();
        item.setClaim(record);
        claimItemMapper.updateByPrimaryKey(item);
    }

    public PageResult query(ClaimQueryObject qo) {
        int count = claimMapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, claimMapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public void changeState(Long id) {
        Claim claim = claimMapper.selectByPrimaryKey(id);
        if (claim.getState()){
            throw new LogicException("数据已审核,无法再次审核");
        }
        claimMapper.changeState(id);
    }

    public PageResult itemQuery(ClaimQueryObject qo) {
        int count = claimItemMapper.query4item4Count(qo);
        if (count > 0) {
            return new PageResult(count, claimItemMapper.query4item4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public String selectStagingByStaging(String staging) {
        return claimMapper.selectStagingByStaging(staging);
    }

    public ClaimItem getCliamItemByClaimId(Long claimId) {
        return claimItemMapper.getCliamItemByClaimId(claimId);
    }

    public void updateByAudit(Claim claim) {
        claimMapper.updateByAudit(claim);
    }

    public List<Employee> selectAllEmp() {
        return claimMapper.selectAllEmp();
    }

    public void update4Dispose(ClaimItem claimItem) {
        claimItem.setDisposeTime(new Date());
        claimItem.setState(false);
        claimItemMapper.update4Dispose(claimItem);

        //保存到场人员ID到审核表
        Long claimItemId = claimItem.getId();
        Long claimId = claimItem.getClaim().getId();
        accidentAuditMapper.upadateEmployeeId(claimItem.getEmployee().getId(),claimItemId,claimId);
    }

    public void changeItemState(Long id) {
        Long employee_id = claimItemMapper.selectEmployeeIdByClaimItem(id);
        if (employee_id == null || employee_id.equals("") ){
            throw new LogicException("请先处理明细再确认处理!");
        }
        ClaimItem claimItem = claimItemMapper.selectByPrimaryKey(id);
        if (claimItem.getState() ){
            throw new LogicException("数据已确认处理成功,无法再次处理");
        }
        claimItemMapper.changeItemState(id);
    }
}
